const express = require('express');
const axios = require('axios');
const router = express.Router();
const cozeToken = require('../modules/cozeToken');

// 执行工作流
router.post('/run', async (req, res) => {
	try {
		console.log('我进入了run', req.body)
    	const { workflow_id, parameters } = req.body
		console.log(typeof parameters)
		console.log(workflow_id, parameters)

		const headers = await cozeToken.getAuthHeaders();
		const response = await axios.post(`https://api.coze.cn/v1/workflow/run`, {
				workflow_id: workflow_id,
				parameters: parameters
			}, {
			headers: headers
    	}).then(response => {
      		console.log('messageCreate', response.data)
			return response.data
		}).catch(error => {
			console.error('API 调用出错:', error);
			return {
				code: 500,
				message: '请求失败',
				success: false
			}
		});
		console.log('1111', response)
		res.json({
			success: true,
			code: 200,
			data: response.data,
			errMsg: '成功'
		})
	} catch (error) {
		console.error('执行工作流失败:', error);
		res.status(500).json({
			success: false,
			code: 500,
			message: '服务器内部错误',
			error: error.message
		});
	}
});

module.exports = router;